const fs = require('fs');
// const path=require('path');
const router = require('koa-router');
const route = router();

let files = fs.readdirSync(__dirname + '/controller');

let filesJs = files.filter((f) => {
    return f.endsWith('.js');
});

for (let f of filesJs) {
    console.log(`process controllers:${f}...`);

    let mapping = require(__dirname + '/controller/' + f);

    for (let url in mapping) {
        if (url.startsWith('GET')) {
            let path = url.substring(4);
            route.get(path, mapping[url]);

            console.log(`register URL mapping: GET${path}`);
        } else if (url.startsWith('POST')) {
            let path = url.substring(5);
            route.post(path, mapping[url]);

            console.log(`register URL mapping: GET${path}`);
        } else {
            // 无效的URL:
            console.log(`invalid URL: ${url}`);
        }
    }
}